package com.mall.db.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.mapper.PmsProductMapper;
import com.mall.db.mapper.SmsFlashPromotionProductRelationMapper;
import com.mall.domain.PmsProduct;
import com.mall.domain.SmsFlashPromotionProductRelation;
import com.mall.interface1.api.SmsFlashPromotionProductRelationApi;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsFlashPromotionProductRelationApiImpl implements SmsFlashPromotionProductRelationApi {

    @Autowired
    private SmsFlashPromotionProductRelationMapper smsFlashPromotionProductRelationMapper;

    @Autowired
    private PmsProductMapper pmsProductMapper;

    //选择商品添加关联
    public void create(SmsFlashPromotionProductRelation smsFlashPromotionProductRelation) {
        smsFlashPromotionProductRelationMapper.insert(smsFlashPromotionProductRelation);
    }

    //根据id删除关联
    public void deleteById(Long id) {
        smsFlashPromotionProductRelationMapper.deleteById(id);
    }

    //根据id分页查询出关联表信息
    public List<SmsFlashPromotionProductRelation> list(Long flashPromotionId, Long flashPromotionSessionId, Integer pageNum, Integer pageSize) {
        Page<SmsFlashPromotionProductRelation> pageInfo = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsFlashPromotionProductRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(flashPromotionId != null,SmsFlashPromotionProductRelation::getFlashPromotionId,flashPromotionId);
        queryWrapper.eq(flashPromotionSessionId != null,SmsFlashPromotionProductRelation::getFlashPromotionSessionId,flashPromotionSessionId);
        Page<SmsFlashPromotionProductRelation> sfpprPage = smsFlashPromotionProductRelationMapper.selectPage(pageInfo, queryWrapper);
        return sfpprPage.getRecords();
    }

    @Override
    public PmsProduct findById(Long productId) {
        return pmsProductMapper.selectById(productId);
    }

    //获取关联商品总数
    public Integer count(Long flashPromotionId, Long flashPromotionSessionId) {
        LambdaQueryWrapper<SmsFlashPromotionProductRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(flashPromotionId != null,SmsFlashPromotionProductRelation::getFlashPromotionId,flashPromotionId);
        queryWrapper.eq(flashPromotionSessionId != null,SmsFlashPromotionProductRelation::getFlashPromotionSessionId,flashPromotionSessionId);
        return smsFlashPromotionProductRelationMapper.selectCount(queryWrapper);
    }

    //修改关联信息
    public void update(SmsFlashPromotionProductRelation smsFlashPromotionProductRelation) {
        smsFlashPromotionProductRelationMapper.updateById(smsFlashPromotionProductRelation);
    }

    //获取管理商品促销信息
    public SmsFlashPromotionProductRelation find(Long id) {
        return smsFlashPromotionProductRelationMapper.selectById(id);
    }
}
